import { ElMessage, ElLoading } from 'element-plus'

const request = (url: string = '', params: any = {}): Promise<void> => {
  console.log('请求URL', url)
  console.log('请求参数', params)

  // const loading = ElLoading.service({
  //   lock: true,
  //   text: '加载中..',
  //   background: 'rgba(0, 0, 0, 0.7)'
  // })

  // 这一段只在本地调试的时候打开，项目部署后一定要注释掉
  // setTimeout(() => {
  //   loading.close()
  // }, 80)

  try {
    return new Promise((resolve, reject) => {
      CefSharp.BindObjectAsync('zw')
        .then(() => {
          zw.command(url, JSON.parse(JSON.stringify(params)))
            .then((res) => {
              console.log('请求结果', res.Data)

              // loading.close()
              const { Code, Msg, Data } = res
              if (Code == 0) {
                resolve(Data)
              } else if (!!Msg) {
                ElMessage.error(Msg)
              } else {
                ElMessage.error('请求异常，请重试～')
              }
            })
            .catch((er: any) => {
              // loading.close()
              reject(er)
            })
        })
        .catch((err: any) => {
          // loading.close()
          reject(err)
        })
    })
  } catch (error) {
    // loading.close()
  }
}

export default request
